package com.o2o.cleaning.month.platform.ebusiness_plat.kuaishou

import java.sql.ResultSet
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.util.{Calendar, Date}

import com.alibaba.fastjson.JSON
import com.google.gson.JsonParser
import org.apache.spark.sql.SparkSession

object test {

  private val time: LocalDate = LocalDate.now()

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("KuaiShou_LiveStreaming")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("ERROR")
    val year = 2021
    val month = 8
    val collection = s"kuaishou_webcast_shop_list_2108"
    val result_path = s"s3a://o2o-dataproces-group/zyf/livestreaming/kuaishou/resultData/${year}/${month}/${collection}/"
    val source_path = s"s3a://o2o-dataproces-group/zyf/livestreaming/kuaishou/sourceData/${year}/${month}/${collection}/"
    val address_path = s"s3a://o2o-dataproces-group/zyf/livestreaming/kuaishou/addressData/${year}/${month}/kuaishou_address/"
    val mid = s"s3a://o2o-dataproces-group/zyf/livestreaming/kuaishou/resultData/${year}/${month}/mid/"
    //    if (0 == 1) println(0)
    //    println(1)

    //    spark.read.orc(s"s3a://o2o-sourcedata-2022/obs-source-2022/1/PupuChaoShi/").show(false)
    //    spark.read.orc(s"s3a://o2o-tempdata/zyf/2022/8/PupuChaoShi/")
    //      .printSchema()
    //      .registerTempTable("t1")
    //      .show(false)
    spark.sql(
      """
        |select explode(map_keys( from_json('{"品牌":"汝碧","品名":"dffgk","产地":"中国","面膜分类":"贴片式","化妆品备案编号/注册证号":"粤G妆网备字2018051045","适合肤质":"任何肤质","上市时间":"2021年","功效":"修护提亮肤色保湿","规格类型":"正常规格","是否为特殊用途化妆品":"否","净含量":"5片10片15片","限期使用日期范围":"2023-02-24至2023-08-25","化妆品保质期":"36个月","保质期":"36个月","核心功效":"修护"}', 'map< string,string >') )) as map_tmp
        |""".stripMargin).show(false)
    spark.sql(
      """
        |select  from_json('{"品牌":"汝碧","品名":"dffgk","产地":"中国","面膜分类":"贴片式","化妆品备案编号/注册证号":"粤G妆网备字2018051045","适合肤质":"任何肤质","上市时间":"2021年","功效":"修护提亮肤色保湿","规格类型":"正常规格","是否为特殊用途化妆品":"否","净含量":"5片10片15片","限期使用日期范围":"2023-02-24至2023-08-25","化妆品保质期":"36个月","保质期":"36个月","核心功效":"修护"}', 'map< string,string >')  as map_tmp
        |""".stripMargin).show(false)
    //    import java.sql.DriverManager
    //    val conn = DriverManager.getConnection("jdbc:odps:http://service.cn-beijing.maxcompute.aliyun.com/api?project=O2O_DataWorks&useProjectTimeZone=true",
    //      "LTAI4GKi5AZZD6bxJkuCiypd", "1lgLxpxVQGEkI3fpDUoeqDVnWJnFNO")
    //
    //    val stmt = conn.createStatement()
    //    val sql: String = "SELECT * FROM fact_jd_soday_nanzhuang_df  where dt ='20220605' limit 20"
    //    val rs = stmt.executeQuery(sql)
    //
    //
    //    import addDataframeMember.wrapResultSet
    //    rs.DF.show()

    //    sc.textFile(source_path).map(line=>{
    //    if  (line.contains("sku_id") && line.contains("$n")){
    //      println(line)
    //    }
    //    }).take(10000000)

    //    println(time.getYear)
    //    println(time.getMonthValue)
    //    println(getTimestamp(year, month, 30))
    //    spark.sql(
    //      """
    //        |select md5("asd")
    //        |""".stripMargin).show(false)
    //    spark.read.json("D://11.json").toJSON.rdd.map(line=>{
    //      val jsonArray = new JsonParser().parse(line).getAsJsonArray
    //      for (i <- 0 until jsonArray.size()-1 ){
    //        val jsonObject = JSON.parseObject(jsonArray.get(i).toString)
    //        val children = new JsonParser().parse(jsonObject.get("children").toString).getAsJsonArray
    //        println(children)
    //      }
    //    }).take(1)
    //      .show(false)
    //      .registerTempTable("t1")
    //    spark.sql(
    //      """
    //        |select * from t1
    //        |""".stripMargin).show(false)
    //        for (i <- 0 to 10) {
    //          println((Math.random() * 100).toInt)
    //        }

    //    val line ="{\n    \"product_id\" : \"ewYnMGfej1c\",\n    \"good_id\" : \"ewYnMGfej1c\",\n    \"title\" : \"（W50）众忆袋装兰州牛肉面86g*10袋\",\n    \"coupon\" : null,\n    \"add_to_field\" : [],\n    \"goodUrl\" : \"\",\n    \"source\" : \"{\\\"itemType\\\": 99, \\\"title\\\": \\\"（W50）众忆袋装兰州牛肉面86g*10袋\\\", \\\"showIconList\\\": [], \\\"jumpToken\\\": \\\"https://app.kwaixiaodian.com/merchant/shop/detail?id=1991814228260&promoteId=ks_885434307_666_9805peQE9YA&promoteChannel=1&asUrl=https%3A%2F%2Fwebapp.kuaishou.com&hyId=kwaishop&layoutType=4&visitorId=5Tapf3APg9Q&authorId=OqPyK1BafoE&carrierId=QnEkLhI4EWc&carrierType=2&itemId=ZqUv1tm0iGU&sourceType=99&clientVersion=2147483647.2147483647.0&clientId=0&ksLogRate=0.1&weblogRate=1&loguiRate=1\\\", \\\"currentStock\\\": 0, \\\"sequence\\\": 15, \\\"recordInfo\\\": {\\\"recordStatus\\\": 2, \\\"playUrl\\\": \\\"kwai://merchantinterpretation?title=%EF%BC%88W50%EF%BC%89%E4%BC%97%E5%BF%86%E8%A2%8B%E8%A3%85%E5%85%B0%E5%B7%9E%E7%89%9B%E8%82%89%E9%9D%A286g*10%E8%A2%8B&imageUrls=%5B%7B%22cdn%22%3A%22ali-ec.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Fali-ec.static.yximgs.com%2Fufile%2Fadsocial%2F292a6927-0edc-4148-bf9c-402972e20d0c.jpg%22%7D%2C%7B%22cdn%22%3A%22tx-ec.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Ftx-ec.static.yximgs.com%2Fufile%2Fadsocial%2F292a6927-0edc-4148-bf9c-402972e20d0c.jpg%22%7D%5D&price=19.90&showIconList=%5B%5D&photoUrls=%5B%7B%22cdn%22%3A%22ali-xdcmt.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Fali-xdcmt.static.yximgs.com%2Fmediacloud%2Fmerchant_record%2Frecord_video%2F4f1ab8c392275fee-8af759c929f216d8da1fd759069645cf-mp4_remux.mp4%22%7D%2C%7B%22cdn%22%3A%22js-xdcmt.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Fjs-xdcmt.static.yximgs.com%2Fmediacloud%2Fmerchant_record%2Frecord_video%2F4f1ab8c392275fee-8af759c929f216d8da1fd759069645cf-mp4_remux.mp4%22%7D%5D&coverUrls=%5B%7B%22cdn%22%3A%22js-xdcmt.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Fjs-xdcmt.static.yximgs.com%2Fmediacloud%2Fmerchant_record%2Frecord_video%2F4f1ab8c392275fee-7c9e3f3209253e058278a687501c74c6-first_frame.jpg%22%7D%2C%7B%22cdn%22%3A%22ali-xdcmt.static.yximgs.com%22%2C%22url%22%3A%22http%3A%2F%2Fali-xdcmt.static.yximgs.com%2Fmediacloud%2Fmerchant_record%2Frecord_video%2F4f1ab8c392275fee-7c9e3f3209253e058278a687501c74c6-first_frame.jpg%22%7D%5D&itemSaleType=1&entranceType=3&itemId=ewYnMGfej1c&liveStreamId=OWeVDZKYgo0&jumpUrl=https%3A%2F%2Fapp.kwaixiaodian.com%2Fmerchant%2Fshop%2Fdetail%3Fid%3D1991814228260%26promoteId%3Dks_885434307_666_9805peQE9YA%26promoteChannel%3D1%26asUrl%3Dhttps%253A%252F%252Fwebapp.kuaishou.com%26hyId%3Dkwaishop%26layoutType%3D4%26visitorId%3D5Tapf3APg9Q%26authorId%3DOqPyK1BafoE%26carrierId%3DQnEkLhI4EWc%26carrierType%3D2%26itemId%3DZqUv1tm0iGU%26sourceType%3D99%26clientVersion%3D2147483647.2147483647.0%26clientId%3D0%26ksLogRate%3D0.1%26weblogRate%3D1%26loguiRate%3D1%26serverExpTag%3D1_u%2F1625106464866_epib1\\\"}, \\\"totalStock\\\": 0, \\\"jumpUrl\\\": \\\"https://app.kwaixiaodian.com/merchant/shop/detail?id=1991814228260&promoteId=ks_885434307_666_9805peQE9YA&promoteChannel=1&asUrl=https%3A%2F%2Fwebapp.kuaishou.com&hyId=kwaishop&layoutType=4&visitorId=5Tapf3APg9Q&authorId=OqPyK1BafoE&carrierId=QnEkLhI4EWc&carrierType=2&itemId=ZqUv1tm0iGU&sourceType=99&clientVersion=2147483647.2147483647.0&clientId=0&ksLogRate=0.1&weblogRate=1&loguiRate=1\\\", \\\"extraMap\\\": {\\\"seckillInfo\\\": null, \\\"itemMarketPrice\\\": null, \\\"timeOfSale\\\": 0, \\\"itemSaleType\\\": 1, \\\"itemOriginalPrice\\\": null, \\\"itemMarketingPrice\\\": null, \\\"jumpType\\\": 2}, \\\"displayPrice\\\": \\\"19.90\\\", \\\"imageUrls\\\": [{\\\"url\\\": \\\"http://ali-ec.static.yximgs.com/ufile/adsocial/292a6927-0edc-4148-bf9c-402972e20d0c.jpg?x-oss-process=image/resize,m_mfit,h_270,w_270/format,webp\\\", \\\"cdn\\\": \\\"ali-ec.static.yximgs.com\\\"}, {\\\"url\\\": \\\"http://tx-ec.static.yximgs.com/ufile/adsocial/292a6927-0edc-4148-bf9c-402972e20d0c.jpg?x-oss-process=image/resize,m_mfit,h_270,w_270/format,webp\\\", \\\"cdn\\\": \\\"tx-ec.static.yximgs.com\\\"}], \\\"id\\\": \\\"ewYnMGfej1c\\\", \\\"appletJumpUrl\\\": \\\"\\\"}\",\n    \"is_flagship\" : false,\n    \"images\" : [ \n        {\n            \"url\" : \"http://ali-ec.static.yximgs.com/ufile/adsocial/292a6927-0edc-4148-bf9c-402972e20d0c.jpg?x-oss-process=image/resize,m_mfit,h_270,w_270/format,webp\",\n            \"cdn\" : \"ali-ec.static.yximgs.com\"\n        }, \n        {\n            \"url\" : \"http://tx-ec.static.yximgs.com/ufile/adsocial/292a6927-0edc-4148-bf9c-402972e20d0c.jpg?x-oss-process=image/resize,m_mfit,h_270,w_270/format,webp\",\n            \"cdn\" : \"tx-ec.static.yximgs.com\"\n        }\n    ],\n    \"is_Presale\" : false\n}"
    //
    //    val nObject = JSON.parseObject(line)
    //    println(nObject.getOrDefault("user_id","-1"))
    //
    //    val array_arr = nObject.getJSONArray("add_to_field").toArray()
    //
    //    val array_sort = array_arr.sortBy(x => {
    //      val crawl_date = JSON.parseObject(x.toString).get("crawl_date")
    //      Integer.parseInt(crawl_date.toString)
    //    })
    //    val array_sort = array_arr.sortWith((x, y) => {
    //      val crawl_datex = JSON.parseObject(x.toString).get("crawl_date").toString.toInt
    //      val crawl_datey = JSON.parseObject(y.toString).get("crawl_date").toString.toInt
    //      val sellCountx = JSON.parseObject(x.toString).get("sellCount")
    //      val sellCounty = JSON.parseObject(y.toString).get("sellCount")
    //      println(s"crael_date : ${crawl_datex}+${crawl_datey} , sellCount : ${sellCountx} + ${sellCounty}")
    //      crawl_datex == crawl_datey match {
    //        case true => checkOutSellCount(sellCountx.toString) < checkOutSellCount(sellCounty.toString)
    //        case false => crawl_datex < crawl_datey
    //      }
    //    })

    //    for (arr <- array_sort) {
    //
    //      val json = JSON.parseObject(arr.toString)
    //      println(json.getString("sellCount"))
    //
    //    }
    //
    //    val lastArr = JSON.parseObject(array_sort.last.toString)
    //    val headArr = JSON.parseObject(array_sort.head.toString)
    //
    //    val sellCount = checkOutSellCount(lastArr.getString("sellCount")) - checkOutSellCount(headArr.getString("sellCount"))
    //    println("sellCount :" + sellCount)
  }

  def getDateFormate(timeStamp: String): String = {
    val sdf = new SimpleDateFormat("yyyy-MM-dd");
    var df = sdf.format(new Date((timeStamp + "000").toLong))
    if (timeStamp.length != "1590940800".length) {
      df = sdf.format(new Date((timeStamp).toLong))
    }
    df
  }

  def checkOutSellCount(sellcount: String): Int = {
    var midselld = 0.0
    var midselli = 0
    if (sellcount != null && sellcount != "") {
      if (sellcount.contains("W") || sellcount.contains("w")) {
        midselld = sellcount.dropRight(1).toDouble
        midselli = (midselld * 10000).toInt
      } else {
        midselli = sellcount.toInt
      }
      midselli
    } else {
      0
    }
  }

  /**
   * 获得前几天的时间戳
   *
   * @param month
   * @param day
   * @return
   */
  def getTimestamp(year: Int, month: Int, day: Int): Long = {
    val format = new SimpleDateFormat("yyyy-MM-dd");
    val calendar: Calendar = Calendar.getInstance()
    calendar.set(year, month - 1, day)
    val time: Date = calendar.getTime
    val qyt = format.format(time)
    val timeStamp: Long = format.parse(qyt).getTime
    val result = timeStamp.toString.substring(0, timeStamp.toString.length - 3).toLong
    result
  }
}
